<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="6"><el-button type="primary">添加角色</el-button></el-col>
        <!-- 表格区域 -->
        <el-table :data="roleList" style="width: 100%">
          <!-- 展开行 -->
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row class="top_border" :gutter="10" v-for="item1 in scope.row.children" :key="item1.id">
                <el-col :span="6"
                  ><el-tag @close="deleteRightFn(scope.row.id, item1.id)" closable>{{ item1.authName }}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="18">
                  <el-row class="item2top_border" :gutter="10" v-for="item2 in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag @close="deleteRightFn(scope.row.id, item2.id)" closable type="success">{{ item2.authName }}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <span v-for="item3 in item2.children" :key="item3.id"
                        ><el-tag @close="deleteRightFn(scope.row.id, item3.id)" closable type="warning">{{ item3.authName }}</el-tag></span
                      >
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="roleName" label="角色名称"> </el-table-column>
          <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
              <el-button @click="setRoleRight(scope.row)" size="mini" type="warning" icon="el-icon-setting">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog @close="cleardefkeys" title="分配权限" :visible.sync="rightsdialogVisible" width="50%">
      <!-- 树状列表 -->

      <el-tree ref="treeRights" :data="rightsList" show-checkbox node-key="id" default-expand-all :default-checked-keys="defkeys" :props="rightsdefaultProps"> </el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRoleRightsFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllRolesList, deleteRoleRight, getRightsTreeList, setRoleRights } from '@/api/index.js'
export default {
  data() {
    return {
      //当前角色id
      currentRoleId: '',
      //默认展开的菜单id
      defkeys: [],
      //tree的props指定 子树与展示文本属性
      rightsdefaultProps: {
        children: 'children',
        label: 'authName',
      },
      //树状权限列表
      rightsList: [],
      //分配权限对话框
      rightsdialogVisible: false,
      //角色列表
      roleList: [],
    }
  },
  created() {
    //发送请求获取角色列表
    this.getAllRolesListFn()
    //获取权限列表
    // getRightsTreeList().then(res=>{
    //   console.log(res)
    //  if (res.data.meta.status != 200) {
    //   return this.$message.error('获取权限列表失败')
    // }
    // this.rightsList = res.data.data
    // })
  },
  methods: {
    //发送请求分配权限
    async setRoleRightsFn() {
      let arr = []
      arr = [ ...this.$refs.treeRights.getCheckedKeys(),...this.$refs.treeRights.getHalfCheckedKeys()]
      let rightsStr = arr.join(',')
      let {data:res} = await setRoleRights(this.currentRoleId,rightsStr)
      if(res.meta.status != 200){return this.$message.error('更新权限失败')}
      this.$message.success('更新权限成功')
      this.getAllRolesListFn()
      //关闭弹框
      this.rightsdialogVisible = false
    },
    //初始化默认选中数组
    cleardefkeys() {
      this.defkeys = []
      this.currentRoleId = ''
    },
    //获取当前角色三级权限id
    getRoleRightsid(role) {
      if (!role.children) {
        return this.defkeys.push(role.id)
      }
      role.children.forEach((item) => {
        this.getRoleRightsid(item)
      })
    },
    //为角色分配权限
    async setRoleRight(role) {
      this.currentRoleId = role.id
      //发送请求获取权限列表
      let { data: res } = await getRightsTreeList()
      if (res.meta.status != 200) {
        return this.$message.error('获取权限列表失败')
      }
      this.rightsList = res.data

      //获取当前角色所有三级权限id
      this.getRoleRightsid(role)

      //打开分配权限对话框
      this.rightsdialogVisible = true
    },
    //删除角色指定权限
    async deleteRightFn(roleid, rightid) {
      // 发送请求删除权限
      let { data: res } = await deleteRoleRight(roleid, rightid)
      if (res.meta.status != 200) {
        return this.$message.error('删除权限失败')
      }
      this.$message.success('删除权限成功')
      //数据回显
      this.roleList.forEach((item, index) => {
        if (item.id == roleid) {
          this.roleList[index].children = res.data
        }
      })
    },
    //获取全部角色列表
    async getAllRolesListFn() {
      let { data: res } = await getAllRolesList()
      if (res.meta.status != 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.roleList = res.data
    },
  },
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 12px;
}
.top_border {
  border-top: solid 1px #eee;
  display: flex;
  align-items: center;
}
.item2top_border {
  border-top: solid 1px #eee;
  display: flex;
  align-items: center;
  &:first-child {
    border: none;
  }
}

.el-tag {
  margin: 7px;
}
</style>
